package com.ar4j.bench.ar4j.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ar4j.bench.ar4j.domain.Item;
import com.ar4j.bench.common.domain.IItem;
import com.ar4j.bench.common.service.IItemService;

/**
 * An item service implemented using the AR4J library
 */
@Service
public class ItemAr4jService implements IItemService {
  
  @Autowired
  private Item itemAdapter;

  @Override
  public Long addItem(IItem item) {
    return ((Item) item).save().getId();
  }

  @Override
  public IItem findById(Long id) {
    return itemAdapter.findByIdentifier(id);
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<IItem> findByProductId(Long productId) {
    return (List<IItem>) ((List<?>) itemAdapter.findAllByProductId(productId)); // really dumb trick...bow head in shame
  }

  @Override
  public void removeById(Long id) {
    itemAdapter.deleteByIdentifier(id);    
  }

  @Override
  public void updateItem(IItem item) {
    ((Item) item).save();
  }

}
